﻿using System;
using System.Windows.Forms;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using QuanLyThongTinCoSo.Forms.DonVi;

namespace QuanLyThongTinCoSo.Forms.Doi
{
    public partial class frmDoiList : frmList
    {
        public frmDoiList()
        {
            InitializeComponent();
            dgvDoi.AutoGenerateColumns = false;
            LoadData();
        }

        private void AddNew(object sender, EventArgs e)
        {
            using (var frm = new frmDoiDetail())
            {
                frm.OnLoadData += LoadData;
                frm.ShowDialog();
            }
        }

        protected override void Edit()
        {
            EditData();
        }

        private void EditData()
        {
            if (dgvDoi.SelectedRows.Count > 0)
            {
                var selectedRow = dgvDoi.SelectedRows[0];
                var id = Convert.ToInt32(selectedRow.Cells[0].Value.ToString());

                using (var fEdit = new frmDoiDetail(id))
                {
                    fEdit.OnLoadData += LoadData;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        protected override void LoadData()
        {
            var doiBiz = new DoiBiz();

            dgvDoi.DataSource = null;
            dgvDoi.Refresh();
            dgvDoi.DataSource = doiBiz.GetAll();

            txtTenDoi.Text = string.Empty;
            txtDienGiai.Text = string.Empty;
            txtPhong.Text = string.Empty;
            txtCanBo.Text = string.Empty;
        }

        protected void SelectionChanged(object sender, System.EventArgs e)
        {
            var gv = (DataGridView) sender;
            foreach (DataGridViewRow row in gv.SelectedRows)
            {
                txtTenDoi.Text = row.Cells[1].Value.ToString();
                txtDienGiai.Text = row.Cells[2].Value.ToString();
                txtPhong.Text = row.Cells[3].Value.ToString();
                txtCanBo.Text = row.Cells[4].Value.ToString();
            }
        }

        private void DeleteData()
        {
            try
            {
                if (dgvDoi.SelectedRows.Count != 0)
                {
                    var id = Convert.ToInt32(dgvDoi.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var doiBiz = new DoiBiz();

                        var model = doiBiz.GetItemById(id);
                        doiBiz.Delete(model);
                        LoadData();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.DeleteSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        protected override void Delete()
        {
            DeleteData();
        }
    }
}